﻿#include "CrashHandler.hpp"
#include "client/crashpad_client.h" // Crashpad 的头文件
#include "base/strings/utf_string_conversions.h"

#include <memory>
#include "client/crashpad_info.h"
#include <client/crash_report_database.h>
#include "client/settings.h"
#include <iostream>

bool CrashHandler::Initialize(const CrashHandlerConfig& config) {
    crashpad::CrashpadClient client;
    // 配置 StartHandler 的参数
    bool status = client.StartHandler(
        base::FilePath(base::UTF8ToUTF16(config.handlerPath)),
        base::FilePath(base::UTF8ToUTF16(config.reportsDir)),
        base::FilePath(base::UTF8ToUTF16(config.databasePath)),
        config.url,
        config.annotations,
        config.arguments,
        config.restartable,
        config.asynchronous
    );

    // Initialize the local crash report database
    auto database = crashpad::CrashReportDatabase::Initialize(base::FilePath(base::UTF8ToUTF16(config.reportsDir)));
    if (!database) {
        std::cerr << "Failed to initialize crash report database." << std::endl;
        return false;
    }

    // Configure settings for automatic uploads
    auto settings = database->GetSettings();
    if (!settings) {
        std::cerr << "Failed to get crash report database settings." << std::endl;
        return false;
    }
    settings->SetUploadsEnabled(true); // Toggle auto uploads

    // 自定义转储信息
    crashpad::CrashpadInfo* crashpad_info = crashpad::CrashpadInfo::GetCrashpadInfo();
    crashpad_info->set_gather_indirectly_referenced_memory(crashpad::TriState::kEnabled, 4 * 1024 * 1024); // 捕获间接引用的内存
    
    // 设置简单注释
    crashpad::SimpleStringDictionary* annotations = crashpad_info->simple_annotations();
    if (!annotations) {
        // 如果没有注释模块，则初始化
        annotations = new crashpad::SimpleStringDictionary();
        crashpad_info->set_simple_annotations(annotations);
    }
    annotations->SetKeyValue("mail-user", config.mail.username);
    annotations->SetKeyValue("mail-pass", config.mail.password);
    annotations->SetKeyValue("mail-from", config.mail.from);
    annotations->SetKeyValue("mail-rcpt", config.mail.rcpt);
    annotations->SetKeyValue("mail-subject", config.mail.subject);
    annotations->SetKeyValue("mail-attachment", config.mail.attachment);

    return status;
}

void CrashHandler::SetLogFile(const std::string& log)
{
    // 自定义转储信息
    crashpad::CrashpadInfo* crashpad_info = crashpad::CrashpadInfo::GetCrashpadInfo();

    // 设置简单注释
    crashpad::SimpleStringDictionary* annotations = crashpad_info->simple_annotations();
    if (!annotations) {
        // 如果没有注释模块，则初始化
        annotations = new crashpad::SimpleStringDictionary();
        crashpad_info->set_simple_annotations(annotations);
    }
    annotations->SetKeyValue("mail-attachment", log);
}

void CrashHandler::SetExtInfo(const std::string& key, const std::string& value)
{
    // 自定义转储信息
    crashpad::CrashpadInfo* crashpad_info = crashpad::CrashpadInfo::GetCrashpadInfo();

    // 设置简单注释
    crashpad::SimpleStringDictionary* annotations = crashpad_info->simple_annotations();
    if (!annotations) {
        // 如果没有注释模块，则初始化
        annotations = new crashpad::SimpleStringDictionary();
        crashpad_info->set_simple_annotations(annotations);
    }

    //设置关键字段
    annotations->SetKeyValue(key, value);
}
